<?php

/**
 * Controller for Reports.
 */
class ReportController extends Zend_Controller_Action
{
    
    /**
     * Set ACL.
     */
    public function init()
    {
	$this->_helper->acl->allow('admin');
    }
    
    public function indexAction()
    {
	
    }
    
    public function childweekAction()
    {
	$mr = new Model_Report;
	$week = (int)$_POST['week'];
	$this ->view->week = $week;
	$this->view->vchild = $mr->getSupervisedChildrenForWeek($week);
	
	$this->renderPDF("Kinder-Kalenderwoche-$week.pdf", $this->renderHtml('childweek.pdf.phtml'));
    }
    
    public function employeeweekAction()
    {
	$mr = new Model_Report;
	$week = (int)$_POST['week'];
	$this ->view->week = $week;
	$this->view->vemployee = $mr->getSupervisedEmployeeForWeek($week);
	
	$this->renderPDF("Betreuer-Kalenderwoche-$week.pdf", $this->renderHtml('employeeweek.pdf.phtml'));
    }
    
     public function festerterminAction()
    {
	$mr = new Model_Report;
	$week = (int)$_POST['week'];
	$this ->view->week = $week;
	$this->view->event = $mr->getFesterTermin($week);
	
	
	$this->renderPDF("Alle-festen-Termine-$week.pdf", $this->renderHtml('festerTermin.pdf.phtml'));
    }
    
    public function abgelehntAction()
    {
	$mr = new Model_Report;
	
	$year = (int)$_POST['year'];
	$this->view->year = $year;
	$this->view->event = $mr->getAbgelehntDate($year);
	
	$this->renderPDF("Abgelehnt.pdf", $this->renderHtml('abgelehnt.pdf.phtml'));
    }
    
    public function monthlyAction()
    {
	$mr = new Model_Report;
	$year = (int)$_POST['year'];
	$this->view->year = $year;
	$this->view->months = $mr->getMonthlyReportForYear($year);
	
	$this->renderPDF("Monats-Report-$year.pdf", $this->renderHtml('monthly.pdf.phtml'));
    }
    
    public function employeeavaiableAction()
    {
	$mr = new Model_Report();
	$year = (int)$_POST['year'];
	$week = (int)$_POST['week'];
	$this->view->week = $week;
	$this->view->year = $year;
	$this->view->result = $mr->getAvailableEmployeeReportForWeek($week, $year);
	
	$this->renderPDF("Mitarbeiter-Verfuegbar-Report-$week-$year.pdf", $this->renderHtml('employeeavaiable.pdf.phtml'));
    }
    
    public function employeeAction()
    {
	$mr = new Model_Report();
	$year = (int)$_POST['year'];
	$this->view->year = $year;
	$this->view->data = $mr->getEmployeeMonthlyReportForYear($year);
	$this->view->user = $mr->getAllEmployees();
	
	$this->renderPDF("Mitarbeiter-Monats-Report-$year.pdf", $this->renderHtml('employee.pdf.phtml'), "landscape");
    }
    
    /**
     * Render Html Content of a view script.
     * 
     * @param string $fileName the view script's file name.
     * @return string the rendered content.
     */
    private function renderHtml($fileName)
    {
	$lh = Zend_Layout::getMvcInstance();
	$lh->content = $this->view->render("report/$fileName");
	return $lh->render('pdf');
    }
    
    /**
     * Stream a pdf file to the user.
     * 
     * @param type $filename the PDF's name.
     * @param type $html the PDF's content.
     */
    private function renderPDF($filename, $html, $option = "portrait")
    {
	$this->_helper->layout()->disableLayout(); 
	$this->_helper->viewRenderer->setNoRender(true);
	
	$dompdf = new DOMPDF();  
	$dompdf->set_paper("a4",$option);  
	$dompdf->load_html($html);  
	$dompdf->render(); 
	$dompdf->stream($filename);
    }
}